"""Populates the datastore with characters from UnicodeChars.csv.
"""
from __future__ import division
from google.appengine.ext import db
import csv
import optparse

import fieldnames
from ui import models
from scripts import remote
import util

# Swallows the ValueError exception thrown from int()
@util.ignore_exception(ValueError)
def int_try_parse(*args, **kwargs):
  return int(*args, **kwargs)

class BatchDatastorePopulator(util.BatchProcessor):
  def execute(self, workitems):
    db.put(workitems)

def main():
  parser = optparse.OptionParser()
  parser.add_option("-r", "--remote", default=False, action="store_true",
                    help="Run this script on the remote instance of this app")
  (options, args) = parser.parse_args()
  args # silence warning "args is never used"
  
  if options.remote:
    servername = remote.SERVER_REMOTE
  else:
    servername = remote.SERVER_LOCAL
  
  reader = csv.DictReader(util.opendatafile("UnicodeChars.csv"),
                          fieldnames=fieldnames.destination, delimiter=",",
                          quotechar='"')
  try:
    batch_size = 25 # remote.BATCH_SIZE
    print("Populate datastore with UnicodeChar entities.")
    batch = BatchDatastorePopulator(batch_size)
    remote.connect(servername)
    print("Processing %i at a time" % batch_size)
    
    for line in reader:
      char = models.UnicodeChar()
      char.code = int(line["code"])
      char.name = line["name"]
      char.html_entity = line["html_entity"]
      char.block = line["block"]
      char.category_code = line["category_code"]
      char.category = line["category"]
      char.combining = int(line["combining"])
      char.bidi_code = line["bidi_code"]
      char.bidi = line["bidi"]
      char.decomposition = line["decomposition"]
      char.decimal = int_try_parse(line["decimal"])
      char.digit = int_try_parse(line["digit"])
      char.numeric = line["numeric"]
      char.mirrored = line["mirrored"]
      char.unicode10 = line["unicode10"]
      char.comment = line["comment"]
      char.upper = int_try_parse(line["upper"], 16)
      char.lower = int_try_parse(line["lower"], 16)
      char.title = int_try_parse(line["title"], 16)
      batch.add(char)
    batch.finish()
    print("Done.")
  except KeyboardInterrupt:
    print("Terminated.")
  
if __name__ == "__main__":
  main()
